// =====================
// The cube pixel shader
// =====================

// The texture sampler 
sampler TextureSampler	: register(s0);

// The pixel shader input structure
struct PS_INPUT
{
	float4 TexCoord : TEXCOORD0;	// Texture coordinate
};

// The pixel shader output structure
struct PS_OUTPUT
{
	float4 Color	: COLOR0;		// Output color of the pixel
};

// Our pixel shader
PS_OUTPUT SecondaryPixelShader(PS_INPUT In)
{
	PS_OUTPUT Result;
	
	//float4 TexColor = tex2D( TextureSampler, In.TexCoord);

	//Result.Color = TexColor;// * float4(0.9f,0.3f,0.0f,1.0f); 
	
	
	
	
	const float2 samples[12] = {
		-0.326212, -0.405805,
		-0.840144, -0.073580,
		-0.695914,  0.457137,
		-0.203345,  0.620716,
		 0.962340, -0.194983,
		 0.473434, -0.480026,
		 0.519456,  0.767022,
		 0.185461, -0.893124,
		 0.507431,  0.064425,
		 0.896420,  0.412458,
		-0.321940, -0.932615,
		-0.791559, -0.597705,
	};

	float4 sum = tex2D( TextureSampler, In.TexCoord);
	for (int i = 0; i < 12; i++){
		sum += tex2D( TextureSampler, In.TexCoord + 0.0025 * samples[i]);
	}

	sum /= 13; // 7 light, 13 blur
	Result.Color = 11*sum;

	const float off = 0.001f;

	// Sobel filter
	float4 s00 = tex2D(TextureSampler, In.TexCoord  + float2(-off, -off));
	float4 s01 = tex2D(TextureSampler, In.TexCoord  + float2( 0,   -off));
	float4 s02 = tex2D(TextureSampler, In.TexCoord  + float2( off, -off));

	float4 s10 = tex2D(TextureSampler, In.TexCoord  + float2(-off,  0));
	float4 s12 = tex2D(TextureSampler, In.TexCoord  + float2( off,  0));

	float4 s20 = tex2D(TextureSampler, In.TexCoord  + float2(-off,  off));
	float4 s21 = tex2D(TextureSampler, In.TexCoord + float2( 0,    off));
	float4 s22 = tex2D(TextureSampler, In.TexCoord  + float2( off,  off));

	float4 sobelX = s00 + 2 * s10 + s20 - s02 - 2 * s12 - s22;
	float4 sobelY = s00 + 2 * s01 + s02 - s20 - 2 * s21 - s22;

	float4 edgeSqr = sobelX * sobelX + sobelY * sobelY;
	Result.Color += 1 - dot(edgeSqr, 80);
	
	Result.Color/=12;


	
	return Result;
}